﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Lenovo.CFI.Common;
using Lenovo.CFI.Common.Mail;
using System.Net.Mail;

namespace Lenovo.CFI.DAL.Mail
{
    public class MessageDa
    {
        /// <summary>
        /// 向邮件队列插入邮件。
        /// </summary>
        /// <param name="mail">邮件对象。</param>
        public static void InsertMailQueue(Message mail)
        {
            InsertMailQueue(mail, DBHelper.GetDatabase(), null);
        }
        public static void InsertMailQueue(Message mail, TranscationHelper trans)
        {
            InsertMailQueue(mail, trans.DataBase, trans.Transaction);
        }
        private static void InsertMailQueue(Message mail, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_InsertMailQueue",
                mail.UID,
                mail.System,
                mail.From,
                mail.To,
                mail.CC,
                mail.BCC,
                mail.ReplyTo,
                mail.Sender,
                (int)mail.Priority,
                mail.Subject,
                mail.Body,
                mail.CreateTime,
                mail.Status);
            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }
    }
}
